import { Component, OnInit, OnDestroy, AfterViewInit } from '@angular/core';
import * as Enumerable from 'linq';
import { NzMessageService } from 'ng-zorro-antd';
import { BaseService, DataRegisterService, PageService } from 'src/app/services';
import { EventManagerService } from 'src/app/services/event-manager.service';
import { OrganizationService } from 'src/app/services/organization.service';
import { UploadService } from 'src/app/services/upload.service';
import { PermitConfig, PermitConfigId } from '../../../models';
import { EngineService } from '../../../services/engine.service';
import { BaseFieldComponent } from '../base-field/base-field.component';

@Component({
  selector: 'zepride-file-maker',
  templateUrl: './file-maker.component.html',
  styleUrls: ['./file-maker.component.less']
})
export class FileMakerComponent extends BaseFieldComponent implements OnInit, AfterViewInit, OnDestroy {

  permitConfig: PermitConfig[] = [
    { mode: 'query', permitId: '查看', permitName: '', authObjectId: '' },
    { mode: 'edit', permitId: '编辑', permitName: '', authObjectId: '' }
  ];

  showContent = true;
  fileList = [];
  fmmCancelEventId;
  selectedFile;
  isAuthorized = true;

  constructor(
    protected engineService: EngineService,
    protected baseSvr: BaseService,
    protected eventSvr: EventManagerService,
    protected orgSvr: OrganizationService,
    public uploadService: UploadService,
    protected msg: NzMessageService,
    protected pageSvr: PageService,
    protected dataRegisterService: DataRegisterService
  ) {
    super(engineService, baseSvr, eventSvr, orgSvr, uploadService, msg, dataRegisterService);
  }

  ngOnInit(): void {
    this.fileList = this.componentData.extends.fields;
    if (this.fileList && this.fileList.length) {
      this.selectedFile = this.fileList[0];
      this.selectedFile.selected = true;
    }
    this.fmmCancelEventId = this.eventSvr.registEvent('fileMaker.make.cancel', () => {
      this.showContent = true;
    });
  }

  ngAfterViewInit() {
    setTimeout(() => {
      const authData = this.setAuthorityData();
      this.zeprideAuthorityData = {
        ...authData,
        afterChecked: this.afterAuthChecked.bind(this)
      };
    }, 0);
  }

  makeFileOnline() {
    this.showContent = false;
    this.eventSvr.callEvents('fileMaker.make', this.selectedFile);
  }

  ngOnDestroy(): void {
    this.eventSvr.removeEvent(this.fmmCancelEventId);
  }

  selectFile(value) {
    this.fileList.forEach(item => {
      item.selected = false;
    });
    this.selectedFile = value;
    this.selectedFile.selected = true;
  }

  async getMakedFile() {
    const r = await this.getNewFileUrl(4);
    window.open(r.data);
  }

  async getPDFFile() {
    const r = await this.getNewFileUrl(3);
    window.open(r.data);
  }

  private async getNewFileUrl(fileType) {
    // const url = encodeURI(this.selectedFile.fileUrl);
    const url = this.selectedFile.fileUrl;

    let ft = 'word';
    if (this.selectedFile.fileName.indexOf('doc') > -1) {
      ft = 'word';
    }
    else if (this.selectedFile.fileName.indexOf('xls') > -1) {
      ft = 'excel';
    }

    const r: any = await this.pageSvr.makeFile(this.dataModel, url, fileType, ft);
    return r;
  }

  downloadTmpFile(item) {
    window.open(item.fileUrl);
  }

  afterAuthChecked(params: {
    permitId: PermitConfigId;
    isAuthorized: boolean;
  }) {
    if (!params.isAuthorized) {
      this.isAuthorized = false;
    }
  }
}
